package from_0_to_50;

public class Q006_convert {
    public static void main(String[] args) {
        String s = "PAYPALISHIRING";
        String result;
        result = convert(s,1);

        System.out.println(result);
    }
    public static String convert(String s, int numRows) {  //PAHNAPLSIIGYIR
        StringBuffer str = new StringBuffer();
        if(numRows == 1){
            return s;
        }else {
            int sums = 2*(numRows-1);
            for(int j = 0; j < numRows; j++){ // [0,numRows)
                for(int i = 0; i <= s.length() / sums; i++){ // 分段数
                    int left = j + i*sums;  // 左
                    int right = sums - j + i*sums; // 右

                    if(left < s.length()){
                        str.append(s.charAt(left));
                    }
                    if((j != 0 && j != numRows-1) && right < s.length()){
                        str.append(s.charAt(right));
                    }
                }
            }
            return str.toString();
        }
    }
}
